import { _decorator, Component, Node, Label, ProgressBar, Sprite, SpriteFrame } from 'cc';
import { PlayerStats } from './PlayerStats';
import { BuildingType, PlayerType, UnitType } from '../../../battle/Enums';
import { BuildingConfig } from 'db://assets/battle/Const';
import { BattleResultData } from '../../../battle/Model';
import { LOG } from '../../../ConsoleLogCtrl';
const { ccclass, property } = _decorator;

/**
 * 玩家统计项目组件
 * 用于控制单个玩家统计信息的显示和更新
 */
@ccclass('PlayerStatsItem')
export class PlayerStatsItem extends Component {

    // 玩家ID标签
    @property(Label)
    public useridLabel: Label = null;

    // 己方标签
    @property(Label)
    public selfLabel: Label = null;

    // 胜利/失败状态图片
    @property(Sprite)
    public selfStatusSprite: Sprite = null;

    // 己方资源财富RMB
    @property(Label)
    public selfRMB: Label = null;

    // 己方击杀总数
    @property(Label)
    public selfKills: Label = null;

    // 己方攻击伤害
    @property(Label)
    public selfDamages: Label = null;

    // 己方治疗总量
    @property(Label)
    public selfHeals: Label = null;

    // 己方单位统计汇总标签
    @property(Label)
    public selfUnitSummaryLabel: Label = null;       // 单位统计

    // 己方单位统计标签
    @property(Label)
    public selfSoldierLabel: Label = null;           // 战士兵: 全部/存活
    @property(Label)
    public selfCollectLabel: Label = null;           // 采集兵: 全部/存活
    @property(Label)
    public selfDoctorLabel: Label = null;            // 治疗兵: 全部/存活
    @property(Label)
    public selfArcherLabel: Label = null;            // 弓箭手: 全部/存活
    @property(Label)
    public selfWorkerLabel: Label = null;            // 建造兵: 全部/存活

    // 己方建筑统计汇总标签
    @property(Label)
    public selfBuildingSummaryLabel: Label = null;   // 建筑统计

    // 己方建筑统计标签
    @property(Label)
    public selfBaseLabel: Label = null;              // 基地: 全部/存活
    @property(Label)
    public selfTowerLabel: Label = null;             // 防御塔: 全部/存活
    @property(Label)
    public selfWallLabel: Label = null;              // 城墙: 全部/存活

    // 敌方标签
    @property(Label)
    public enemyLabel: Label = null;

    // 敌方资源财富RMB
    @property(Label)
    public enemyRMB: Label = null;

    // 敌方击杀总数
    @property(Label)
    public enemyKills: Label = null;

    // 敌方攻击伤害
    @property(Label)
    public enemyDamageDealt: Label = null;

    // 敌方治疗总量
    @property(Label)
    public enemyHealing: Label = null;

    // 敌方单位统计汇总标签
    @property(Label)
    public enemyUnitSummaryLabel: Label = null;      // 单位统计

    // 敌方单位统计标签
    @property(Label)
    public enemySoldierLabel: Label = null;          // 战士兵: 全部/存活
    @property(Label)
    public enemyCollectLabel: Label = null;          // 采集兵: 全部/存活
    @property(Label)
    public enemyDoctorLabel: Label = null;           // 治疗兵: 全部/存活
    @property(Label)
    public enemyArcherLabel: Label = null;           // 弓箭手: 全部/存活
    @property(Label)
    public enemyWorkerLabel: Label = null;           // 建造兵: 全部/存活

    // 敌方建筑统计汇总标签
    @property(Label)
    public enemyBuildingSummaryLabel: Label = null;  // 建筑统计

    // 敌方建筑统计标签
    @property(Label)
    public enemyBaseLabel: Label = null;             // 基地: 全部/存活
    @property(Label)
    public enemyTowerLabel: Label = null;            // 防御塔: 全部/存活
    @property(Label)
    public enemyWallLabel: Label = null;             // 城墙: 全部/存活

    // 敌方胜利/失败状态图片
    @property(Sprite)
    public enemyStatusSprite: Sprite = null;

    // 胜利图片资源
    @property(SpriteFrame)
    public winSpriteFrame: SpriteFrame = null;

    // 失败图片资源
    @property(SpriteFrame)
    public loseSpriteFrame: SpriteFrame = null;

    // 当前玩家统计数据
    private _selfPlayerStats: PlayerStats = null;

    // 敌方玩家统计数据
    private _enemyPlayerStats: PlayerStats = null;

    // 战斗结果数据
    private _battleResult: BattleResultData = null;

    onLoad() {
        this.initLabels();
        this.initStatusSprites();
    }

    /**
     * 初始化状态图片，默认隐藏
     */
    private initStatusSprites() {
        // 初始化时隐藏胜利/失败状态图片
        if (this.selfStatusSprite) {
            this.selfStatusSprite.node.active = false;
        }
        if (this.enemyStatusSprite) {
            this.enemyStatusSprite.node.active = false;
        }
    }

    /**
     * 初始化标签组件
     * 如果没有手动绑定，则自动查找子节点中的Label组件
     */
    private initLabels() {
        if (!this.useridLabel) {
            const useridNode = this.node.getChildByName('userid');
            if (useridNode) {
                this.useridLabel = useridNode.getComponent(Label);
            }
        }

        if (!this.selfLabel) {
            const selfNode = this.node.getChildByName('self');
            if (selfNode) {
                this.selfLabel = selfNode.getComponent(Label);
            }
        }

        if (!this.selfRMB) {
            const selfRMBNode = this.node.getChildByName('selfRmb');
            if (selfRMBNode) {
                this.selfRMB = selfRMBNode.getComponent(Label);
            }
        }

        // 初始化己方战斗统计标签
        if (!this.selfKills) {
            const selfKillsNode = this.node.getChildByName('selfKills');
            if (selfKillsNode) {
                this.selfKills = selfKillsNode.getComponent(Label);
            }
        }

        if (!this.selfDamages) {
            const selfDamagesNode = this.node.getChildByName('selfDamages');
            if (selfDamagesNode) {
                this.selfDamages = selfDamagesNode.getComponent(Label);
            }
        }

        if (!this.selfHeals) {
            const selfHealsNode = this.node.getChildByName('selfHeals');
            if (selfHealsNode) {
                this.selfHeals = selfHealsNode.getComponent(Label);
            }
        }

        // 初始化己方单位统计汇总标签
        if (!this.selfUnitSummaryLabel) {
            const node = this.node.getChildByName('selfUnits');
            if (node) this.selfUnitSummaryLabel = node.getComponent(Label);
        }

        // 初始化己方单位统计标签
        if (!this.selfSoldierLabel) {
            const node = this.node.getChildByName('selfSoldiers');
            if (node) this.selfSoldierLabel = node.getComponent(Label);
        }
        if (!this.selfCollectLabel) {
            const node = this.node.getChildByName('selfCollects');
            if (node) this.selfCollectLabel = node.getComponent(Label);
        }
        if (!this.selfDoctorLabel) {
            const node = this.node.getChildByName('selfDoctors');
            if (node) this.selfDoctorLabel = node.getComponent(Label);
        }
        if (!this.selfArcherLabel) {
            const node = this.node.getChildByName('selfArchers');
            if (node) this.selfArcherLabel = node.getComponent(Label);
        }
        if (!this.selfWorkerLabel) {
            const node = this.node.getChildByName('selfWorkers');
            if (node) this.selfWorkerLabel = node.getComponent(Label);
        }

        // 初始化己方建筑统计汇总标签
        if (!this.selfBuildingSummaryLabel) {
            const node = this.node.getChildByName('selfBuildings');
            if (node) this.selfBuildingSummaryLabel = node.getComponent(Label);
        }

        // 初始化己方建筑统计标签
        if (!this.selfBaseLabel) {
            const node = this.node.getChildByName('selfBases');
            if (node) this.selfBaseLabel = node.getComponent(Label);
        }
        if (!this.selfTowerLabel) {
            const node = this.node.getChildByName('selfTowers');
            if (node) this.selfTowerLabel = node.getComponent(Label);
        }
        if (!this.selfWallLabel) {
            const node = this.node.getChildByName('selfWalls');
            if (node) this.selfWallLabel = node.getComponent(Label);
        }

        if (!this.enemyLabel) {
            const enemyNode = this.node.getChildByName('enemy');
            if (enemyNode) {
                this.enemyLabel = enemyNode.getComponent(Label);
            }
        }

        if (!this.enemyRMB) {
            const enemyRMBNode = this.node.getChildByName('enemyRmb');
            if (enemyRMBNode) {
                this.enemyRMB = enemyRMBNode.getComponent(Label);
            }
        }

        // 初始化敌方战斗统计标签
        if (!this.enemyKills) {
            const enemyKillsNode = this.node.getChildByName('enemyKills');
            if (enemyKillsNode) {
                this.enemyKills = enemyKillsNode.getComponent(Label);
            }
        }

        if (!this.enemyDamageDealt) {
            const enemyDamageDealtNode = this.node.getChildByName('enemyDamageDealt');
            if (enemyDamageDealtNode) {
                this.enemyDamageDealt = enemyDamageDealtNode.getComponent(Label);
            }
        }

        if (!this.enemyHealing) {
            const enemyHealingNode = this.node.getChildByName('enemyHealing');
            if (enemyHealingNode) {
                this.enemyHealing = enemyHealingNode.getComponent(Label);
            }
        }

        // 初始化敌方单位统计汇总标签
        if (!this.enemyUnitSummaryLabel) {
            const node = this.node.getChildByName('enemyUnits');
            if (node) this.enemyUnitSummaryLabel = node.getComponent(Label);
        }

        // 初始化敌方单位统计标签
        if (!this.enemySoldierLabel) {
            const node = this.node.getChildByName('enemySoldiers');
            if (node) this.enemySoldierLabel = node.getComponent(Label);
        }
        if (!this.enemyCollectLabel) {
            const node = this.node.getChildByName('enemyCollects');
            if (node) this.enemyCollectLabel = node.getComponent(Label);
        }
        if (!this.enemyDoctorLabel) {
            const node = this.node.getChildByName('enemyDoctors');
            if (node) this.enemyDoctorLabel = node.getComponent(Label);
        }
        if (!this.enemyArcherLabel) {
            const node = this.node.getChildByName('enemyArchers');
            if (node) this.enemyArcherLabel = node.getComponent(Label);
        }
        if (!this.enemyWorkerLabel) {
            const node = this.node.getChildByName('enemyWorkers');
            if (node) this.enemyWorkerLabel = node.getComponent(Label);
        }

        // 初始化敌方建筑统计汇总标签
        if (!this.enemyBuildingSummaryLabel) {
            const node = this.node.getChildByName('enemyBuildings');
            if (node) this.enemyBuildingSummaryLabel = node.getComponent(Label);
        }

        // 初始化敌方建筑统计标签
        if (!this.enemyBaseLabel) {
            const node = this.node.getChildByName('enemyBases');
            if (node) this.enemyBaseLabel = node.getComponent(Label);
        }
        if (!this.enemyTowerLabel) {
            const node = this.node.getChildByName('enemyTowers');
            if (node) this.enemyTowerLabel = node.getComponent(Label);
        }
        if (!this.enemyWallLabel) {
            const node = this.node.getChildByName('enemyWalls');
            if (node) this.enemyWallLabel = node.getComponent(Label);
        }

    }

    /**
     * 设置玩家统计数据并更新显示
     * @param playerStats 玩家统计数据
     * @param enemyPlayerStats 敌方玩家统计数据
     */
    public setPlayerStats(playerStats: PlayerStats, enemyPlayerStats: PlayerStats) {
        this._selfPlayerStats = playerStats;
        this._enemyPlayerStats = enemyPlayerStats;
        this.updateDisplay();
    }

    /**
     * 设置战斗结果数据
     * @param battleResult 战斗结果数据
     */
    public setBattleResult(battleResult: BattleResultData) {
        this._battleResult = battleResult;
        this.updateBattleResultDisplay();
    }

    /**
     * 更新显示内容
     */
    public updateDisplay() {
        if (!this._selfPlayerStats) {
            LOG.warn('PlayerStatsItem: 没有设置玩家统计数据');
            return;
        }

        // 更新玩家ID
        if (this.useridLabel) {
            this.useridLabel.string = `玩家: ${this._selfPlayerStats.getId()}`;
        }

        // 更新己方信息
        if (this.selfLabel) {
            this.selfLabel.string = `${this._selfPlayerStats.getId()}`;
            let label = this.selfLabel.node.getChildByName("Label").getComponent(Label);
            let bar = this.selfLabel.node.getChildByName('ProgressBar').getComponent(ProgressBar);
            label.string = `${this._selfPlayerStats.getBaseHealth()}/${BuildingConfig[BuildingType.base].hp}`;
            const hpRatio = this._selfPlayerStats.getBaseHealth() / BuildingConfig[BuildingType.base].hp;
            bar.progress = Math.max(0, Math.min(1, hpRatio));
        }

        // 更新己方资源财富RMB
        if (this.selfRMB && this._selfPlayerStats) {
            this.selfRMB.string = `经济财富: ${this._selfPlayerStats.getRmb()}`;
        }

        // 更新己方战斗统计数据
        if (this.selfKills && this._selfPlayerStats) {
            this.selfKills.string = `击  杀  数: ${this._selfPlayerStats.getKills()}`;
        }

        if (this.selfDamages && this._selfPlayerStats) {
            this.selfDamages.string = `伤  害  量: ${this._selfPlayerStats.getTotalDamageDealt()}`;
        }

        if (this.selfHeals && this._selfPlayerStats) {
            this.selfHeals.string = `治  疗  量: ${this._selfPlayerStats.getTotalHealing()}`;
        }

        // 更新己方单位统计汇总
        if (this.selfUnitSummaryLabel && this._selfPlayerStats) {
            const totalAlive = this._selfPlayerStats.getAllUnits().length;
            const totalDead = this._selfPlayerStats.getDeadUnitsCountByType(UnitType.soldier) +
                             this._selfPlayerStats.getDeadUnitsCountByType(UnitType.collect) +
                             this._selfPlayerStats.getDeadUnitsCountByType(UnitType.doctor) +
                             this._selfPlayerStats.getDeadUnitsCountByType(UnitType.archer) +
                             this._selfPlayerStats.getDeadUnitsCountByType(UnitType.worker);
            const totalAll = totalAlive + totalDead;
            this.selfUnitSummaryLabel.string = `单位统计: ${totalAlive}/${totalAll}`;
        }

        // 更新己方单位统计 - 全部/存活格式
        if (this.selfSoldierLabel && this._selfPlayerStats) {
            const aliveCount = this._selfPlayerStats.getAllUnits().filter(u => u.type === UnitType.soldier).length;
            const deadCount = this._selfPlayerStats.getDeadUnitsCountByType(UnitType.soldier);
            const totalCount = aliveCount + deadCount;
            this.selfSoldierLabel.string = `战  士  兵: ${aliveCount}/${totalCount}`;
        }
        if (this.selfCollectLabel && this._selfPlayerStats) {
            const aliveCount = this._selfPlayerStats.getAllUnits().filter(u => u.type === UnitType.collect).length;
            const deadCount = this._selfPlayerStats.getDeadUnitsCountByType(UnitType.collect);
            const totalCount = aliveCount + deadCount;
            this.selfCollectLabel.string = `采  集  兵: ${aliveCount}/${totalCount}`;
        }
        if (this.selfDoctorLabel && this._selfPlayerStats) {
            const aliveCount = this._selfPlayerStats.getAllUnits().filter(u => u.type === UnitType.doctor).length;
            const deadCount = this._selfPlayerStats.getDeadUnitsCountByType(UnitType.doctor);
            const totalCount = aliveCount + deadCount;
            this.selfDoctorLabel.string = `治  疗  兵: ${aliveCount}/${totalCount}`;
        }
        if (this.selfArcherLabel && this._selfPlayerStats) {
            const aliveCount = this._selfPlayerStats.getAllUnits().filter(u => u.type === UnitType.archer).length;
            const deadCount = this._selfPlayerStats.getDeadUnitsCountByType(UnitType.archer);
            const totalCount = aliveCount + deadCount;
            this.selfArcherLabel.string = `弓  箭  手: ${aliveCount}/${totalCount}`;
        }
        if (this.selfWorkerLabel && this._selfPlayerStats) {
            const aliveCount = this._selfPlayerStats.getAllUnits().filter(u => u.type === UnitType.worker).length;
            const deadCount = this._selfPlayerStats.getDeadUnitsCountByType(UnitType.worker);
            const totalCount = aliveCount + deadCount;
            this.selfWorkerLabel.string = `建  造  兵: ${aliveCount}/${totalCount}`;
        }

        // 更新己方建筑统计汇总
        if (this.selfBuildingSummaryLabel && this._selfPlayerStats) {
            const totalAlive = this._selfPlayerStats.getAllBuildings().length;
            const totalDead = this._selfPlayerStats.getDestroyedBuildingsCountByType(BuildingType.base) +
                             this._selfPlayerStats.getDestroyedBuildingsCountByType(BuildingType.tower) +
                             this._selfPlayerStats.getDestroyedBuildingsCountByType(BuildingType.wall);
            const totalAll = totalAlive + totalDead;
            this.selfBuildingSummaryLabel.string = `建筑统计: ${totalAlive}/${totalAll}`;
        }

        // 更新己方建筑统计 - 全部/存活格式
        if (this.selfBaseLabel && this._selfPlayerStats) {
            const aliveCount = this._selfPlayerStats.getAllBuildings().filter(b => b.type === BuildingType.base).length;
            const deadCount = this._selfPlayerStats.getDestroyedBuildingsCountByType(BuildingType.base);
            const totalCount = aliveCount + deadCount;
            this.selfBaseLabel.string = `基       地: ${aliveCount}/${totalCount}`;
        }
        if (this.selfTowerLabel && this._selfPlayerStats) {
            const aliveCount = this._selfPlayerStats.getAllBuildings().filter(b => b.type === BuildingType.tower).length;
            const deadCount = this._selfPlayerStats.getDestroyedBuildingsCountByType(BuildingType.tower);
            const totalCount = aliveCount + deadCount;
            this.selfTowerLabel.string = `防  御  塔: ${aliveCount}/${totalCount}`;
        }
        if (this.selfWallLabel && this._selfPlayerStats) {
            const aliveCount = this._selfPlayerStats.getAllBuildings().filter(b => b.type === BuildingType.wall).length;
            const deadCount = this._selfPlayerStats.getDestroyedBuildingsCountByType(BuildingType.wall);
            const totalCount = aliveCount + deadCount;
            this.selfWallLabel.string = `城       墙: ${aliveCount}/${totalCount}`;
        }

        // 更新敌方信息
        if (this.enemyLabel) {
            this.enemyLabel.string = `${this._enemyPlayerStats.getId()}`;
            let label = this.enemyLabel.node.getChildByName("Label").getComponent(Label);
            let bar = this.enemyLabel.node.getChildByName('ProgressBar').getComponent(ProgressBar);
            label.string = `${this._enemyPlayerStats.getBaseHealth()}/${BuildingConfig[BuildingType.base].hp}`
            const hpRatio = this._enemyPlayerStats.getBaseHealth() / BuildingConfig[BuildingType.base].hp;
            bar.progress = Math.max(0, Math.min(1, hpRatio));
        }

        // 更新敌方资源财富RMB
        if (this.enemyRMB && this._enemyPlayerStats) {
            this.enemyRMB.string = `经济财富: ${this._enemyPlayerStats.getRmb()}`;
        }

        // 更新敌方战斗统计数据
        if (this.enemyKills && this._enemyPlayerStats) {
            this.enemyKills.string = `击  杀  数: ${this._enemyPlayerStats.getKills()}`;
        }

        if (this.enemyDamageDealt && this._enemyPlayerStats) {
            this.enemyDamageDealt.string = `伤  害  量: ${this._enemyPlayerStats.getTotalDamageDealt()}`;
        }

        if (this.enemyHealing && this._enemyPlayerStats) {
            this.enemyHealing.string = `治  疗  量: ${this._enemyPlayerStats.getTotalHealing()}`;
        }

        // 更新敌方单位统计汇总
        if (this.enemyUnitSummaryLabel && this._enemyPlayerStats) {
            const totalAlive = this._enemyPlayerStats.getAllUnits().length;
            const totalDead = this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.soldier) +
                             this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.collect) +
                             this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.doctor) +
                             this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.archer) +
                             this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.worker);
            const totalAll = totalAlive + totalDead;
            this.enemyUnitSummaryLabel.string = `单位统计: ${totalAlive}/${totalAll}`;
        }

        // 更新敌方单位统计 - 全部/存活格式
        if (this.enemySoldierLabel && this._enemyPlayerStats) {
            const aliveCount = this._enemyPlayerStats.getAllUnits().filter(u => u.type === UnitType.soldier).length;
            const deadCount = this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.soldier);
            const totalCount = aliveCount + deadCount;
            this.enemySoldierLabel.string = `战  士  兵: ${aliveCount}/${totalCount}`;
        }
        if (this.enemyCollectLabel && this._enemyPlayerStats) {
            const aliveCount = this._enemyPlayerStats.getAllUnits().filter(u => u.type === UnitType.collect).length;
            const deadCount = this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.collect);
            const totalCount = aliveCount + deadCount;
            this.enemyCollectLabel.string = `采  集  兵: ${aliveCount}/${totalCount}`;
        }
        if (this.enemyDoctorLabel && this._enemyPlayerStats) {
            const aliveCount = this._enemyPlayerStats.getAllUnits().filter(u => u.type === UnitType.doctor).length;
            const deadCount = this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.doctor);
            const totalCount = aliveCount + deadCount;
            this.enemyDoctorLabel.string = `治  疗  兵: ${aliveCount}/${totalCount}`;
        }
        if (this.enemyArcherLabel && this._enemyPlayerStats) {
            const aliveCount = this._enemyPlayerStats.getAllUnits().filter(u => u.type === UnitType.archer).length;
            const deadCount = this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.archer);
            const totalCount = aliveCount + deadCount;
            this.enemyArcherLabel.string = `弓  箭  手: ${aliveCount}/${totalCount}`;
        }
        if (this.enemyWorkerLabel && this._enemyPlayerStats) {
            const aliveCount = this._enemyPlayerStats.getAllUnits().filter(u => u.type === UnitType.worker).length;
            const deadCount = this._enemyPlayerStats.getDeadUnitsCountByType(UnitType.worker);
            const totalCount = aliveCount + deadCount;
            this.enemyWorkerLabel.string = `建  造  兵: ${aliveCount}/${totalCount}`;
        }

        // 更新敌方建筑统计汇总
        if (this.enemyBuildingSummaryLabel && this._enemyPlayerStats) {
            const totalAlive = this._enemyPlayerStats.getAllBuildings().length;
            const totalDead = this._enemyPlayerStats.getDestroyedBuildingsCountByType(BuildingType.base) +
                             this._enemyPlayerStats.getDestroyedBuildingsCountByType(BuildingType.tower) +
                             this._enemyPlayerStats.getDestroyedBuildingsCountByType(BuildingType.wall);
            const totalAll = totalAlive + totalDead;
            this.enemyBuildingSummaryLabel.string = `建筑统计: ${totalAlive}/${totalAll}`;
        }

        // 更新敌方建筑统计 - 全部/存活格式
        if (this.enemyBaseLabel && this._enemyPlayerStats) {
            const aliveCount = this._enemyPlayerStats.getAllBuildings().filter(b => b.type === BuildingType.base).length;
            const deadCount = this._enemyPlayerStats.getDestroyedBuildingsCountByType(BuildingType.base);
            const totalCount = aliveCount + deadCount;
            this.enemyBaseLabel.string = `基       地: ${aliveCount}/${totalCount}`;
        }
        if (this.enemyTowerLabel && this._enemyPlayerStats) {
            const aliveCount = this._enemyPlayerStats.getAllBuildings().filter(b => b.type === BuildingType.tower).length;
            const deadCount = this._enemyPlayerStats.getDestroyedBuildingsCountByType(BuildingType.tower);
            const totalCount = aliveCount + deadCount;
            this.enemyTowerLabel.string = `防  御  塔: ${aliveCount}/${totalCount}`;
        }
        if (this.enemyWallLabel && this._enemyPlayerStats) {
            const aliveCount = this._enemyPlayerStats.getAllBuildings().filter(b => b.type === BuildingType.wall).length;
            const deadCount = this._enemyPlayerStats.getDestroyedBuildingsCountByType(BuildingType.wall);
            const totalCount = aliveCount + deadCount;
            this.enemyWallLabel.string = `城       墙: ${aliveCount}/${totalCount}`;
        }

        // 更新战斗结果显示
        this.updateBattleResultDisplay();
    }

    /**
     * 更新战斗结果显示
     */
    private updateBattleResultDisplay() {
        if (!this._battleResult || !this._selfPlayerStats) {
            return;
        }

        const winner = this._battleResult.winner;
        const selfPlayerId = this._selfPlayerStats.getId();
        const enemyPlayerId = this._enemyPlayerStats ? this._enemyPlayerStats.getId() : null;

        // 更新己方状态
        if (this.selfStatusSprite) {
            if (winner === selfPlayerId) {
                // 己方胜利
                this.selfStatusSprite.spriteFrame = this.winSpriteFrame;
                this.selfStatusSprite.node.active = true;
            } else if (winner && winner !== selfPlayerId) {
                // 己方失败
                this.selfStatusSprite.spriteFrame = this.loseSpriteFrame;
                this.selfStatusSprite.node.active = true;
            } else {
                // 未结束
                this.selfStatusSprite.node.active = false;
            }
        }

        // 更新敌方状态
        if (this.enemyStatusSprite && enemyPlayerId) {
            if (winner === enemyPlayerId) {
                // 敌方胜利
                this.enemyStatusSprite.spriteFrame = this.winSpriteFrame;
                this.enemyStatusSprite.node.active = true;
            } else if (winner && winner !== enemyPlayerId) {
                // 敌方失败
                this.enemyStatusSprite.spriteFrame = this.loseSpriteFrame;
                this.enemyStatusSprite.node.active = true;
            } else {
                // 未结束
                this.enemyStatusSprite.node.active = false;
            }
        }
    }

    /**
     * 获取当前玩家统计数据
     * @returns 玩家统计数据
     */
    public getPlayerStats(): PlayerStats {
        return this._selfPlayerStats;
    }

    /**
     * 清空显示内容
     */
    public clearDisplay() {
        this._selfPlayerStats = null;
        this._enemyPlayerStats = null;
        this._battleResult = null;

        // 清空所有标签文本
        if (this.useridLabel) this.useridLabel.string = '';
        if (this.selfLabel) this.selfLabel.string = '';
        if (this.selfRMB) this.selfRMB.string = '';

        // 清空己方战斗统计标签
        if (this.selfKills) this.selfKills.string = '';
        if (this.selfDamages) this.selfDamages.string = '';
        if (this.selfHeals) this.selfHeals.string = '';

        // 清空己方单位统计汇总标签
        if (this.selfUnitSummaryLabel) this.selfUnitSummaryLabel.string = '';

        // 清空己方单位统计标签
        if (this.selfSoldierLabel) this.selfSoldierLabel.string = '';
        if (this.selfCollectLabel) this.selfCollectLabel.string = '';
        if (this.selfDoctorLabel) this.selfDoctorLabel.string = '';
        if (this.selfArcherLabel) this.selfArcherLabel.string = '';
        if (this.selfWorkerLabel) this.selfWorkerLabel.string = '';

        // 清空己方建筑统计汇总标签
        if (this.selfBuildingSummaryLabel) this.selfBuildingSummaryLabel.string = '';

        // 清空己方建筑统计标签
        if (this.selfBaseLabel) this.selfBaseLabel.string = '';
        if (this.selfTowerLabel) this.selfTowerLabel.string = '';
        if (this.selfWallLabel) this.selfWallLabel.string = '';

        if (this.enemyLabel) this.enemyLabel.string = '';
        if (this.enemyRMB) this.enemyRMB.string = '';

        // 清空敌方战斗统计标签
        if (this.enemyKills) this.enemyKills.string = '';
        if (this.enemyDamageDealt) this.enemyDamageDealt.string = '';
        if (this.enemyHealing) this.enemyHealing.string = '';

        // 清空敌方单位统计汇总标签
        if (this.enemyUnitSummaryLabel) this.enemyUnitSummaryLabel.string = '';

        // 清空敌方单位统计标签
        if (this.enemySoldierLabel) this.enemySoldierLabel.string = '';
        if (this.enemyCollectLabel) this.enemyCollectLabel.string = '';
        if (this.enemyDoctorLabel) this.enemyDoctorLabel.string = '';
        if (this.enemyArcherLabel) this.enemyArcherLabel.string = '';
        if (this.enemyWorkerLabel) this.enemyWorkerLabel.string = '';

        // 清空敌方建筑统计汇总标签
        if (this.enemyBuildingSummaryLabel) this.enemyBuildingSummaryLabel.string = '';

        // 清空敌方建筑统计标签
        if (this.enemyBaseLabel) this.enemyBaseLabel.string = '';
        if (this.enemyTowerLabel) this.enemyTowerLabel.string = '';
        if (this.enemyWallLabel) this.enemyWallLabel.string = '';

        // 隐藏胜利/失败状态图片
        if (this.selfStatusSprite) this.selfStatusSprite.node.active = false;
        if (this.enemyStatusSprite) this.enemyStatusSprite.node.active = false;
    }
}